package com.java110.mall.shop.cmd.shop;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shop.ShopCommunityDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.shop.IShopCommunityInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "shop.queryShopCommunity")
public class QueryShopCommunityCmd extends Cmd {

    @Autowired
    private IShopCommunityInnerServiceSMO shopCommunityInnerServiceSMOImpl;

    @Autowired
    private IStoreV1InnerServiceSMO storeInnerServiceSMOImpl;


    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        String queryByCommunity = MappingCache.getValue("MALL","QUERY_BY_COMMUNITY");
        String appId= cmdDataFlowContext.getReqHeaders().get("app-id");



        ShopCommunityDto shopCommunityDto = BeanConvertUtil.covertBean(reqJson,ShopCommunityDto.class);
        String openType = reqJson.getString("openType");
        if (!StringUtil.isEmpty(openType) && openType.contains(",")) {
            shopCommunityDto.setOpenTypes(openType.split(","));
            shopCommunityDto.setOpenType("");
        } else {
            shopCommunityDto.setOpenType(openType);
        }

        int count = shopCommunityInnerServiceSMOImpl.queryShopCommunitysCount(shopCommunityDto);

        List<ShopCommunityDto> shopCommunityDtos = null;
        List<ShopDto> shopDtos = null;
        if (count > 0) {
            shopCommunityDtos = shopCommunityInnerServiceSMOImpl.queryShopCommunitys(shopCommunityDto);
            shopDtos = BeanConvertUtil.covertBeanList(shopCommunityDtos,ShopDto.class);
            freshShopImg(shopDtos);
            freshStore(shopDtos);
        } else {
            shopDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopCommunityDto.getRow()), count, shopDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        cmdDataFlowContext.setResponseEntity(responseEntity);

    }

    /**
     * 刷入商户信息
     *
     * @param shopDtos
     */
    private void freshStore(List<ShopDto> shopDtos) {

        if (shopDtos == null || shopDtos.size() < 1) {
            return;
        }
        List<String> storeIds = new ArrayList<>();
        for (ShopDto shopDto : shopDtos) {
            storeIds.add(shopDto.getStoreId());
        }

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreIds(storeIds.toArray(new String[storeIds.size()]));
        List<StoreDto> storeDtos = storeInnerServiceSMOImpl.queryStores(storeDto);

        for (ShopDto shopDto : shopDtos) {
            for (StoreDto tmpStoreDto : storeDtos) {
                if (shopDto.getStoreId().equals(tmpStoreDto.getStoreId())) {
                    shopDto.setStoreName(tmpStoreDto.getName());
                    shopDto.setStoreTel(tmpStoreDto.getTel());
                    shopDto.setStoreAddress(tmpStoreDto.getAddress());
                }
            }
        }
    }

    /**
     * @param shopDtos
     */
    private void freshShopImg(List<ShopDto> shopDtos) {
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (ShopDto shopDto : shopDtos) {
            if(StringUtil.isEmpty(shopDto.getShopLogo())){
                continue;
            }

            if(shopDto.getShopLogo().startsWith("http")){
                continue;
            }
            shopDto.setShopLogo(imgUrl + shopDto.getShopLogo());
        }
    }
}
